package com.study.mqtt.controller;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

	@Autowired
    private MqttClient client;
	
	@RequestMapping("testMqtt")
    public String test() throws MqttException {
		String topic = "test/topic";
		String content = "你好呀，我们都在用免费的mqtt服务器";
		MqttMessage message = new MqttMessage(content.getBytes());
		client.publish(topic, message);
		return "success";
	}
	
	@RequestMapping("test2")
    public String test2() throws MqttException {
		return "test2";
	}
}
